﻿namespace com.stifel.ServerBuildMgmt.BuildMgmtAntiVirusInfo
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using com.stifel.Extensions.StringExtensions;
    using com.stifel.ServerBuildMgmtDao;

    public partial class BuildMgmtAntiVirusInfoUserControl : UserControl
    {
        #region Methods

        protected void onItemCommand( object sender, RepeaterCommandEventArgs e )
        {
            if ( e.CommandName == "Edit" )
            {
                Summary s = new Summary( );
                ServerValues server = e.CommandArgument.ToString( ).FromBase64String( ).DeserializeFromXml<ServerValues>( );
                uiCurrentSever.Value = e.CommandArgument.ToString( );
                Label1.Text = s.getSummaryWithOutOriginalValues( server );
                uiDdlPolicy_Load( server.AntiVirus.Policy );
                if ( server.AntiVirus.CompletedFlag.HasValue )
                {
                    uiCbAvCompletedFlag.Checked = server.AntiVirus.CompletedFlag.Value;
                }
                else
                {
                    uiCbAvCompletedFlag.Checked = false;
                }
                //TextBox1.Text = server.AntiVirus.Text1;
                //TextBox2.Text = server.AntiVirus.Text2;
                //uiValAntiVirusNotes.Text = server.AntiVirus.Note;
                uiAntiVirus.Visible = false;
                uiAntiVirusUpdatePanel.Visible = true;
                setDefaultButton( );
            }
        }

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !Page.IsPostBack )
            {
                isChanged.Value = false.ToString( );
                if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == null )
                {
                    PageLoadBackUP( );
                }
                else if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == string.Empty )
                {
                    PageLoadBackUP( );
                }
                else
                {
                    try
                    {
                        Guid ItemGuid = HttpContext.Current.Request.QueryString[ "ItemGuid" ].ToGuid( );
                        StifelServerBuildDao dao = new StifelServerBuildDao( );
                        StifelVirtualServerRequest request = dao.getRequestFromListUsingGUID( ItemGuid );
                        if ( request != null )
                        {
                            uiLitValRequest.Text = request.Id.Value.ToString( );
                            Summary s = new Summary( );
                            VirtualServerCollectionClass v = request.getRequestInfoDeserialized( );
                            List<RepeaterBinding> summaryBinder = s.getSummaryWithOutOriginalValues( v );
                            if ( request.getAntiVirusStatus( ) == true )
                            {
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                uiAntiVirus.Visible = true;
                                uiAntiVirusUpdatePanel.Visible = false;
                                uiBackBadUrlRequest.Visible = false;
                                setSubmitValidation( );
                            }
                            else
                            {
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                uiAntiVirus.Visible = true;
                                uiAntiVirusUpdatePanel.Visible = false;
                                uiBackBadUrlRequest.Visible = false;
                                foreach ( RepeaterItem r in myRepeater.Items )
                                {
                                    ImageButton ie = ( ImageButton ) r.FindControl( "editButton" );
                                    ie.ImageUrl = "~/_layouts/com.stifel.ServerBuildMgmt/images/editg20x20grey.png";
                                    ie.Enabled = false;
                                }
                                uiBtnSave.Enabled = false;
                                uiBtnSubmit.Enabled = false;
                                uiLitNotification.Text = "Cannot Make Any Changes.";
                            }
                        }
                    }
                    catch ( Exception ex )
                    {
                        PageLoadBackUP( );
                        string errorMsg = ex.Message;
                    }
                }
                isSomethingChanged( );
                setUrlReferrer( );
            }
        }

        protected void uiBtnCancel_Click( object sender, EventArgs e )
        {
            Page.Response.Redirect( UrlReferrer );
        }

        protected void uiBtnSave_Click( object sender, EventArgs e )
        {
            SaveServers( );
        }

        protected void uiBtnSubmit_Click( object sender, EventArgs e )
        {
            SubmitServers( );
        }

        protected void uiCancelButton_Click( object sender, ImageClickEventArgs e )
        {
            uiAntiVirus.Visible = true;
            uiAntiVirusUpdatePanel.Visible = false;
            uiBackBadUrlRequest.Visible = false;
            cleanErrorMsg( );
        }

        protected void uiNavigationTab_Click( object sender, EventArgs e )
        {
            Button button = sender as Button;
            if ( button != null )
            {
                string arg = button.CommandArgument;
                navigateToSelectedView( button );
                //summarize( arg );
            }
        }

        protected void uiOkButton_Click( object sender, ImageClickEventArgs e )
        {
            bool ok = Validation( );
            if ( ok )
            {
                ServerValues server = uiCurrentSever.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
                VirtualServerCollectionClass v = new VirtualServerCollectionClass( );
                foreach ( RepeaterItem r in myRepeater.Items )
                {
                    HiddenField hiddenField = ( HiddenField ) r.FindControl( "valuePart" );
                    ServerValues currentCompare = hiddenField.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
                    if ( currentCompare.ServerBasics.ServerName.Equals( server.ServerBasics.ServerName ) )
                    {
                        currentCompare.AntiVirus.Policy = uiDdlPolicy.SelectedItem.Text.Trim( );
                        if ( uiCbAvCompletedFlag.Checked )
                        {
                            currentCompare.AntiVirus.CompletedFlag = true;
                        }
                        else
                        {
                            currentCompare.AntiVirus.CompletedFlag = false;
                        }
                        //currentCompare.AntiVirus.Text1 = TextBox1.Text;
                        //currentCompare.AntiVirus.Text2 = TextBox2.Text;
                        //currentCompare.AntiVirus.Note = uiValAntiVirusNotes.Text;
                    }
                    v.virtualServerCollection.Add( currentCompare );
                }
                Summary s = new Summary( );
                List<RepeaterBinding> summaryBinder = s.getSummaryWithOutOriginalValues( v );
                myRepeater.DataSource = summaryBinder;
                myRepeater.DataBind( );
                uiAntiVirus.Visible = true;
                uiAntiVirusUpdatePanel.Visible = false;
                uiBackBadUrlRequest.Visible = false;
                isChanged.Value = true.ToString( );
                isSomethingChanged( );
                cleanErrorMsg( );
                setSubmitValidation( );
            }
        }

        #endregion Methods

        #region Other

        //protected void uiDdlPolicy_SelectedIndexChanged( object sender, EventArgs e )
        //{
        //    Validation( );
        //    //summarize( uiBtnAV.CommandArgument );
        //}
        //protected void uiCbAvCompletedFlag_CheckedChanged( object sender, EventArgs e )
        //{
        //    Validation( );
        //    //summarize( uiBtnAV.CommandArgument );
        //}

        #endregion Other
    }
}